package com.gugee.tiktok.deploy.consumer.all.controller;


import com.gugee.tiktok.data.common.msg.MusicMessage;
import com.gugee.tiktok.data.consumer.music.core.service.MusicService;
import com.xiguaji.boot.toolkit.jdbc.JdbcTemplateHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
//@RequestMapping("config")
public class NacosConfigControllerTest {

    @Autowired
    MusicService musicService;

    @Autowired
    @Qualifier("kafkaTemplateMusic")
    private KafkaTemplate<Long, MusicMessage> kafkaTemplateMusic;

    private int page = 1;

    @RequestMapping(path = "/send")
    public void sendFoo(Integer page,Integer pageSize,Integer runCount) {
        update(page,pageSize,runCount);
    }

    public void update(Integer page,Integer pageSize,Integer runCount){
        int count = 1;
        if(null != runCount){
            count+=runCount;
        }

        while(count > 0){
            if(null == pageSize){
                pageSize = 10;
            }

            int start = (page +1) * pageSize;
            List<Map<String,Object>> musics = JdbcTemplateHelper.select("select * from music2 order by user_growth desc limit ?,?",start,pageSize);
            List<MusicMessage> musicMessageList = new ArrayList<>();

            musics.forEach( m ->{
                MusicMessage musicMessage = new MusicMessage();
                musicMessage.setMid(Long.parseLong(m.get("mid").toString()));
                musicMessageList.add(musicMessage);
            });

            musicService.updateMusic(musicMessageList);
            page = page + 1;
            count--;
        }
    }

}
